import ttkbootstrap as ttk
from ttkbootstrap.constants import *
import tkinter as tk
import time,os

class infoWin():
    def __init__(self,main):
        self.main=main
        self.info = ttk.Toplevel(None)
        self.info.protocol("WM_DELETE_WINDOW", self._close)

        # #暂停显示
        # self.info.withdraw()
        # self.info.update()
        
        self.init_layout()
        self.coreData()

        #绘制完成，正常显示
        # self.info.deiconify()

    def _close(self):
        '''
        隐藏
        '''
        self.info.withdraw() #隐藏
    
    def _show(self):
        '''
        显示    
        '''
        self.info.deiconify()

    def init_layout(self):
        '''
        设置窗口样式
        '''
        self._close()
        self.info.overrideredirect(1) 
        self.info.wm_attributes("-alpha", 0.8)        # 透明度(0.0~1.0)
        self.info.wm_attributes("-toolwindow", True) #设置为工具窗口，五右上角按钮
        screenwidth = self.main.win.winfo_screenwidth()*0.8
        screenheight = 100
        self.info.geometry('+'+str(int(screenwidth))+'+'+str(screenheight))  #窗口位置
        self.info.wm_attributes("-topmost", True) 
        self.info.update()

        # self.info.geometry('+'+str(int(screenwidth))+'+'+str(screenheight))  #窗口位置

        self.info.bind("<ButtonPress-1>", self.StartMove) #监听左键按下操作响应函数
        self.info.bind("<ButtonRelease-1>", self.StopMove) #监听左键松开操作响应函数
        self.info.bind("<B1-Motion>", self.OnMotion)   #监听鼠标移动操作响应函数
        self.info.bind("<Escape>", self.close)

    
    def close(self,event):
        '''
        关闭窗口
        '''
        self._close()

    def coreData(self):
        self.mainFrame=ttk.Frame(self.info)
        self.mainFrame.pack(fill=BOTH, expand=YES)

        big_data = ttk.Labelframe(
            master=self.mainFrame,
            text='大数据 2022-08-16',
            padding=(20, 10)
        )
        big_data.pack(side=TOP,pady=10,padx=10,fill=BOTH)

        self.create_form(big_data,"hadoop","https://hadoop.apache.org/,https://github.com/apache/hadoop","3.3.4")
        self.create_form(big_data,"hbase","https://hbase.apache.org/","2.4.13")
        self.create_form(big_data,"kafka","https://kafka.apache.org/","3.2.1")
        self.create_form(big_data,"flink","https://flink.apache.org/zh/","1.15.1")
        self.create_form(big_data,"spark","https://spark.apache.org/,https://github.com/apache/spark","3.3.0")
        self.create_form(big_data,"tez","https://tez.apache.org/,https://archive.apache.org/dist/tez/","0.10.2")

        web = ttk.Labelframe(
            master=self.mainFrame,
            text='web 2022-08-23',
            padding=(20, 10)
        )
        web.pack(side=TOP,pady=10,padx=10,fill=BOTH)

        self.create_form(web,"jdk","https://www.oracle.com/java/technologies/downloads/archive/#JavaSE,https://jdk.java.net/,https://www.injdk.cn/,https://adoptium.net/temurin/releases/","java 17 LST")
        self.create_form(web,"eclipse","https://www.eclipse.org/downloads/packages/","2022-06")
        self.create_form(web,"tomcat","https://tomcat.apache.org/,https://tomcat.apache.org/whichversion.html","9.0.65")
        self.create_form(web,"springboot","https://spring.io/projects/spring-boot,https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-dependencies","2.6.11")

        db = ttk.Labelframe(
            master=self.mainFrame,
            text='数据库 2022-09-08',
            padding=(20, 10)
        )
        db.pack(side=TOP,pady=10,padx=10,fill=BOTH)
        self.create_form(db,"mysql","https://dev.mysql.com/downloads/mysql/,https://downloads.mysql.com/archives/community/","8.0.30")
        self.create_form(db,"redis","https://redis.io/download/","7.0")
    
    def create_form(self,master, name, url,version):
        container = ttk.Frame(master)
        container.pack(fill=X, expand=YES, pady=5)

        lbl = ttk.Label(master=container, text=name, width=10)
        lbl.pack(side=LEFT,anchor=E, padx=5)
        # lbl.bind('<Double-1>',self.openSoure) #双击 双击运行前，先执行单击

        
        lbl.bind('<Double-1>',lambda event: self.openSource(event,url)) #双击 双击运行前，先执行单击

        ent = ttk.Label(master=container, text=version)
        ent.pack(side=LEFT, padx=5, fill=X, expand=YES)

    def openSource(self,event,data):
        if "," in data:
            tmp=data.split(",")
            for one in tmp:
                os.system("start "+one)
        else:
            os.system("start "+data)


    #-------窗口移动-------
    def StartMove(self,event):
        global x, y
        x = event.x
        y = event.y
    
    def StopMove(self,event):
        global x, y
        x = None
        y = None
    
    def OnMotion(self,event):
        global x, y
        deltax = event.x - x
        deltay = event.y - y
        self.info.geometry("+%s+%s" % (self.info.winfo_x() + deltax, self.info.winfo_y() + deltay))
        self.info.update()
    # print(event.x,event.y,root.winfo_x(),root.winfo_y(),root.winfo_width(),root.winfo_height())
 